<?php 

namespace app\common\controller;

use think\Controller;

class Home extends Controller {
        /**
     * 无需登录的方法
     * @var array
     */
    protected $noNeedLogin = [];

    // 用户存储信息
    protected $Business = [];

    public function _initialize() {
        // 当前操作名
        $action = $this->request->action();

        $this->BusinessModel = model('Business.Business');

        if(!in_array($action, $this->noNeedLogin) && !in_array('*', $this->noNeedLogin)) {
            $this -> IsLogin();
        }
    }

    /**
     * 判断是否登录
     * @param $redirect 重定向参数 true为默认跳转 false为不跳转
     */
    public function IsLogin($redirect = true) {
        $Business = session('business') ?  session('business') : [];

        if(empty($Business))
        {
            $this->error('未登陆，请先去登陆', 'home/index/login');
            exit;
        }

        $id = isset($Business['id']) ? $Business['id'] : 0;
        $mobile = isset($Business['mobile']) ? $Business['mobile'] : 0;

        $Businessinfo = $this->BusinessModel->where(['id' => $id, 'mobile' => $mobile])->find();

        if(!$Businessinfo)
        {
            // 删除（当前作用域）
            session('business', null);
            $this->error('未登陆，请先去登陆', 'home/index/login');
            exit;
        }
        
        // 模板变量赋值
        $this->view->assign('Business', $Business);

        // 赋值控制器全局使用
        $this->Business = $Businessinfo;

        return $Business;
    }
}

?>